var app = getApp(),
  fn = app.fn;

Page({
  data: {

    startTime: '',
    carts: [],
    orderPro: [],
    time: null,
    shipping_way: 1,
    picket_select: true,

    addressData: {},

    addressStatus: 0,
    isRemote: 0,
    remark: '',

    couponData: {},

    couponStatus: 0,

    total: '',

    combined: '',

    espressPrice: '',

    num: '',

    cids: '',

    messages: '',
    msgPrefix: '',
  },

  onLoad: function(option) {

    this.setData({
      startTime: this.getCurrentTime()
    })


  
    this.setData({
      pid: option.id,

    })

    if (option.remark != null) {

      this.setData({

          remark: option.remark ,
         "orderPro.address_id": option.address_id
 
        }

      )

    }


    this.loadData();

  },
  bindTimeChange: function(e) {
    this.setData({
      time: e.detail.value
    })
  },
  getCurrentTime: function(e) {
    var date = new Date();
    var hour = date.getHours();
    var minute = date.getMinutes();
    var second = date.getSeconds();
    return hour + ":" + minute;
  },

  selectShipping: function(e) {

    this.setData({
      shipping_way: e.currentTarget.dataset.index,
      "orderPro.shipping_id": e.currentTarget.dataset.id,
    });

    this.loadData();
  },
  selectPicketBonus: function(e) {
    let that = this;
    this.setData({
      bonus_id: e.currentTarget.dataset.id,
    });
  },

  selectPicket: function() {
    let that = this;
    this.setData({
      picket_select: !that.data.picket_select
    });
  },

  sum: function() {
    var carts = this.data.carts;

    var total = 0;

    var num = 0;

    for (var i = 0; i < carts.length; i++) {
      total += carts[i].productNumber * carts[i].price;
      num += carts[i].productNumber;
    }

    var espressPrice = this.data.espressPrice;
    var addressData = this.data.addressData;
    var isRemote = this.data.isRemote;
    var couponData = this.data.couponData;
    var couponStatus = this.data.couponStatus;


    if (isRemote == 0) {

      espressPrice = '0';
    }


    var combined = 0;
    combined = total + parseInt(espressPrice);


    if (couponStatus == 1) {

      combined = combined - couponData.m;
      combined = parseFloat(combined).toFixed(1)
    }


    this.setData({
      total: total,
      combined: combined,
      espressPrice: espressPrice,
      num: num
    });
  },

  on() {
    var that = this;

    if (!that.data.addressData.consignee) {

      app.showMsg('请设置收货地址');
      return;
    }

    fn.apiData({
      url: 'flow.php',
      data: {
        act: 'done',
        user_id: app.globalData.uid,
        supplier_id: that.data.pid,
        address_id: that.data.addressData.address_id,
        shipping_id: that.data.orderPro.shipping_id,
        bonus_id: that.data.bonus_id,
        postscript: that.data.remark,
        best_time: '',
      },

      success: function(res) {


        if (res.data.code == 0) {


          that.requestPayment(res.data.data);
        } else {

          app.showMsg(res.data.msg);
        
        }

      }
    });
  },


  requestPayment: function(e) {

    wx.requestPayment({
      'timeStamp': e.timestamp,
      'nonceStr': e.nonceStr,
      'package': e.package,
      'signType': e.signType,
      'paySign': e.paySign,
      'success': function(res) {

        app.showMsg('支付成功！');
        app.globalData.payStatus = 'success';
        var url;
        url = '../orderList/orderList';

        wx.switchTab({
          url: url
        })


      },
      'fail': function(res) {

        app.showMsg('取消付款!');
        app.globalData.payStatus = 'fail';
        var url = '../orderList/orderList';
        setTimeout(function(){
          wx.switchTab({
            url: url
          })
        },1500);
        
      }
    })
  },


  bindconfirm: function(e) {
    var that = this;
    that.setData({
      messages: e.detail.value,
    });
  },


  bindAddress: function(e) {
    var that = this;
    var url;
    url = '../myaddress/myaddress?block=' + that.data.pid;
    wx.navigateTo({
      url: url
    })
  },


  addressBlock: function(e) {

    var that = this;

    that.setData({
      addressData: e,

      addressStatus: 1,

      "orderPro.address_id": e.address_id

    });

    this.loadData();
    // 统计金额
    // that.sum();
  },


  bindCoupon: function(e) {
    var url;
    var that = this;
    url = '../coupon/coupon?block=1&prices=' + that.data.combined;
    wx.navigateTo({
      url: url
    })
  },


  couponBlock: function(e) {
    var that = this;

    that.setData({
      couponData: e,
      couponStatus: 1,
    });
    // 统计金额
    that.sum();
  },

  /** 加载数据 */



  loadData: function() {
    var that = this;

    that.setData({

      "orderPro.act": 'checkout',
      "orderPro.supplier_id": that.data.pid,
      "orderPro.user_id": app.globalData.uid,

    });

    fn.apiData({
      url: 'flow.php',
      data: that.data.orderPro,
      success: function(res) {

        wx.hideToast();

        if (res.data.code == 0) {


          that.setData({
            carts: res.data.data,
            addressData: res.data.data.consignee,
            'orderPro.shipping_id': res.data.data.shipping_list[0].shipping_id

          })
          if (res.data.data.bonus_list.length > 0) {

            that.setData({
              bonus_id: res.data.data.bonus_list[0].bonus_id,
            })
          }




        } else {

          app.showMsg(res.data.msg);
          console.log(res.data.msg + "错误信息");

          setTimeout(function() {


            if (res.data.code == 50007) {
              var url = '../addAddress/addAddress?block=' + that.data.pid;
              wx.navigateTo({
                url: url
              })

            } else {
              wx.navigateBack({
               delta: 1
               })

            }
          }, 2000)
        }
      }
    });
  },


  loadAddressData: function() {
    var that = this;
    fn.apiData({
      url: 'defaultAddress.php',
      data: {
        uid: that.data.uid,
      },
      success: function(res) {

        if (res.data.success) {
          that.addressBlock(res.data.result);
        }
      }
    });
  },

})